package com.th.supcom.pds.internal.client.widget;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.enums.MedicareCardTypeEnum;
import com.th.supcom.pds.internal.client.common.PdsInternalServiceFactory;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.service.MedicareCardRpcAsync;

public abstract class EmployeeCardField extends BaseCardField
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";;
    private static final MedicareCardRpcAsync s_MEDICARECARDRPCASYNC = PdsInternalServiceFactory.getMedicareCardService ();

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private PatientInfoModel m_medicareCardAllInfoModel;

    public EmployeeCardField ()
    {
        m_medicareCardAllInfoModel = new PatientInfoModel ();

        // 初始化TextField
        addCardListener ();
    }

    /**
     * 添加医疗卡刷卡触发事件
     */
    private void addCardListener ()
    {
        addListener (Events.Change, new Listener <BaseEvent> ()
        {
            @Override
            public void handleEvent (final BaseEvent be)
            {
                if (null == getValue ())
                {
                    MessageBox.alert (CodeConstants.PROMPT, "职工卡号为空!", null);
                    return;
                }

                EmployeeCardField.s_MEDICARECARDRPCASYNC.getMedicareAllCard (getValue (), MedicareCardTypeEnum.EmployeeCard.getEnumCode (), "0",
                                                                             new AsyncCallback <PatientInfoModel> ()
                                                                             {

                                                                                 @Override
                                                                                 public void onFailure (final Throwable caught)
                                                                                 {
                                                                                     MessageBox.alert (CodeConstants.PROMPT, caught.getMessage (), null);
                                                                                 }

                                                                                 @Override
                                                                                 public void onSuccess (final PatientInfoModel result)
                                                                                 {
                                                                                     m_medicareCardAllInfoModel = result;
                                                                                     handleEventAfter ();
                                                                                 }
                                                                             });
            }
        });
    }

    /**
     * 获取医疗卡患者信息及卡信息
     * 
     * @return
     */
    @Override
    public PatientInfoModel getModelValue ()
    {
        return m_medicareCardAllInfoModel;
    }
}
